<?php
namespace app\common\service\freight;




use app\admin\model\freight\ExpressCity;
use app\admin\model\freight\ExpressDelivery;
use app\admin\model\freight\ExpressFree;
use app\admin\model\UserAddress;
use app\api\model\freight\ExpressShippingMethod;
use app\common\service\BaseService;

class ExpressService extends BaseService
{

    /**
     * 运费模板添加
     * Author:Yingbo Sun
     * @param $post
     */
    public static function expressCreate($post)
    {
        if ($post['is_postage'] == 0){
            unset($post['charge']);
            $ea_express_delivery['template_name'] = $post['template_name'];
            $ea_express_delivery['default_pieces'] = $post['default_pieces'];
            $ea_express_delivery['default_price'] = $post['default_price'];
            $ea_express_delivery['add_pieces'] = $post['add_pieces'];
            $ea_express_delivery['add_price'] = $post['add_price'];
            $ea_express_delivery['is_postage'] = $post['is_postage'];
            //添加运费模板
            $cityName = implode(",",$post['pinkage']['fast_address']);
            try {
                $res = ExpressDelivery::create($ea_express_delivery);
                $ea_express_free = [];
                //运费模板id
                $ea_express_free['template_id'] = $res->id;
                $ea_express_free['address'] = $cityName;
                $ea_express_free['pieces'] = array_unique($post['pinkage']['pieces'])[0];
                $ea_express_free['pieces_price'] = array_unique($post['pinkage']['pieces_price'])[0];
                $save = ExpressFree::create($ea_express_free);
            }catch (\Exception $e){
                return false;
            }
            return $save;
        }else{
            unset($post['pinkage']);
            $ea_express_delivery['template_name'] = $post['template_name'];
            $ea_express_delivery['is_postage'] = $post['is_postage'];
            try {
                $res = ExpressDelivery::create($ea_express_delivery);
                $count = count($post['charge']['fast_address']);
                for ($i = 0; $i < $count; $i++) {
                    $data[$i]['template_id'] = $res->id;
                    $data[$i]['fast_address'] = $post['charge']['fast_address'][$i];
                    $data[$i]['first_pieces'] = $post['charge']['first_pieces'][$i];
                    $data[$i]['first_amount'] = $post['charge']['first_amount'][$i];
                    $data[$i]['add_pieces'] = $post['charge']['add_pieces'][$i];
                    $data[$i]['add_amount'] = $post['charge']['add_amount'][$i];
                }
                $save = ExpressShippingMethod::insertAll($data);
            }catch (\Exception $e){
                return false;
            }

            return $save;
        }
    }

    /**
     * Author:Yingbo Sun
     * @param $addr int 邮寄地址id
     * @param $pieces int 邮寄件数
     * @param $expressTemplateId int 运费模板id
     * @return int|string
     */
    public static function getFreight($addrId,$pieces,$expressTemplateId)
    {
        $freight = 0;
        $address = UserAddress::where('id',$addrId)->find();
        if (!empty($address)){
            //获取省
            $city = ExpressCity::where('id',$address->province_region)->find();
            $region = $city->city_name;
        }else{
            return false;
        }
        $expressTemplate = ExpressDelivery::where('id',$expressTemplateId)->find();
        //包邮
        if ($expressTemplate->is_postage == 0){
            $freight = 0;
            return $freight;
        }
        $where[] = ['template_id','=',$expressTemplateId];
        $where[] = ['fast_address','=',$region];
        $expressShippingMethod = ExpressShippingMethod::where($where)->find();
        if (empty($expressShippingMethod)){
            //没有规定城市 按默认规则计算
            if ($expressTemplate->default_pieces >= $pieces){
                $freight = $expressShippingMethod->default_price;
                return $freight;
            }else{
                //计算出续件的个数
                $add_pieces = bcsub($pieces,$expressTemplate->default_pieces,2);
                //计算出一共多少续件
                $add_price = bcdiv($add_pieces,$expressTemplate->add_pieces,2);
                //计算出续件多少钱
                $price = bcmul($add_price,$expressTemplate->add_price,2);
                //计算出总共多少钱
                $freight = bcadd($price,$expressTemplate->default_price,2);
                return $freight;
            }
        }else{
            //按照指定地区的快递费用规则计算
            if ($expressShippingMethod->first_pieces >= $pieces){
                $freight = $expressShippingMethod->first_amount;
                return $freight;
            }else{
                //计算出续件的个数
                $add_pieces = bcsub($pieces,$expressShippingMethod->first_pieces,2);
                //计算出一共多少续件
                $add_price = bcdiv($add_pieces,$expressShippingMethod->add_pieces,2);
                //计算出续件多少钱
                $price = bcmul($add_price,$expressShippingMethod->add_amount,2);
                //计算出总共多少钱
                $freight = bcadd($price,$expressShippingMethod->first_amount,2);
                return $freight;
            }
        }

    }
}
